package org.dromara.module.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.PageUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.module.domain.Parameter;
import org.dromara.module.service.ParameterService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 配置参数
 *
 * @author Mr.Zhou
 * @date 2025-02-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/module/parameter")
public class ParameterController extends BaseController {

    private final ParameterService parameterService;

    /**
     * 查询配置参数列表
     */
    @SaCheckPermission("module:parameter:list")
    @GetMapping("/list")
    public TableDataInfo<Parameter> list(Parameter parameter, PageQuery pageQuery) {
        QueryWrapper<Parameter> qw = new QueryWrapper<>();

        TableDataInfo<Parameter> td = PageUtils.buildDataInfo(parameterService.page(PageUtils.buildPage(),qw));
        List<Parameter> list = td.getRows();
        return td;
    }

    /**
     * 导出配置参数列表
     */
    @SaCheckPermission("module:parameter:export")
    @Log(title = "配置参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(Parameter parameter, HttpServletResponse response) {
        List<Parameter> list = parameterService.list();
        ExcelUtil.exportExcel(list, "配置参数", Parameter.class, response);
    }

    /**
     * 获取配置参数详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("module:parameter:query")
    @GetMapping("/{id}")
    public R<Parameter> getInfo(@NotNull(message = "主键不能为空") @PathVariable Integer id) {
        Parameter parameter = parameterService.getById(id);
        if(parameter != null){

        }
        return R.ok(parameter);
    }

    /**
     * 新增配置参数
     */
    @SaCheckPermission("module:parameter:add")
    @Log(title = "配置参数", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody Parameter parameter) {
        return toAjax(parameterService.save(parameter));
    }

    /**
     * 修改配置参数
     */
    @SaCheckPermission("module:parameter:edit")
    @Log(title = "配置参数", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody Parameter parameter) {
        parameterService.updateById(parameter);
        parameterService.resetParameter(parameter.getId());
        return toAjax(1);
    }

    /**
     * 删除配置参数
     *
     * @param ids 主键串
     */
    @SaCheckPermission("module:parameter:remove")
    @Log(title = "配置参数", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Integer[] ids) {
        return toAjax(parameterService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
